package health.lm.com.mine.model;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import health.lm.com.data.App;
import health.lm.com.data.HttpCST;
import health.lm.com.mine.entity.AboutUsEntity;
import health.lm.com.mine.entity.AllianceBusinessBean;
import health.lm.com.mine.entity.CardCenterEntity;
import health.lm.com.mine.entity.CardInfoEntity;
import health.lm.com.mine.entity.GrowthIntegralEntity;
import health.lm.com.mine.entity.InstructionEntity;
import health.lm.com.mine.entity.IntegralDetailListEntity;
import health.lm.com.mine.entity.IntegralRankingListEntity;
import health.lm.com.mine.entity.JiFenEntity;
import health.lm.com.mine.entity.MineTotalBean;
import health.lm.com.mine.entity.MyActivitiesEntity;
import health.lm.com.mine.entity.RecordListBean;
import health.lm.com.mine.entity.ServiceEntity;
import health.lm.com.mine.entity.SettingBean;
import health.lm.com.mine.entity.ShareEntity;
import health.lm.com.mine.entity.ThinkTankEntity;
import health.lm.com.mine.entity.ThinkTankItemEntity;
import health.lm.com.mine.entity.UploadEntity;
import health.lm.com.mine.entity.UploadHeadBean;
import health.lm.com.mine.entity.WalletEntity;
import health.lm.com.network.EasyHttp;
import health.lm.com.network.callback.SimpleCallBack;
import health.lm.com.network.utils.MyApi;

/**
 *
 */
public class MineModel {
    private static MineModel mallModel;

    public static MineModel getInstance() {
        if (null == mallModel) {
            mallModel = new MineModel();
        }
        return mallModel;
    }

    //上传
    public void uploadImages(List<String> strings, String token, SimpleCallBack<UploadEntity> observer) {
        List<File> files = new ArrayList<>();
        for (String path : strings) {
            files.add(new File(path));
        }
        uploadFiles(files, token, observer);
    }

    /**
     * 发送短信验证码
     */
    public void sendMsgCode(String mobile, int type, SimpleCallBack<Object> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put("mobile", mobile);
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
//            jsonObject.put(HttpCST.TYPE, type);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_UTILS, HttpCST.INTFC_1001, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    public void uploadFiles(List<File> fileList, String token, SimpleCallBack<UploadEntity> observer) {
        EasyHttp.post(HttpCST.UPLOAD_URL)
                .params(HttpCST.ACCESS_TOKEN, App.model.getAccess_token())
                .addFileParams("image[]", fileList, null)
                .execute(observer);
    }

    /**
     * 我的首页数据
     */
    public void mineFragment(SimpleCallBack<MineTotalBean> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST.USER_ID, App.getModel().getUid());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_BUSSINESS, HttpCST.INTFC_1004, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 设置页面数据
     */
    public void setting(SimpleCallBack<SettingBean> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1006, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 我的 分享接口
     */
    public void getShareData(String phone, SimpleCallBack<ShareEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put("phone", phone);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_BUSSINESS, HttpCST.INTFC_1008, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 进入钱包页面
     */
    public void walletData(String password, SimpleCallBack<WalletEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put("second_password", password);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1035, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 修改密码
     *
     * @param observer observer
     */
    public void changePsw(String oldPsw, String newPsw, String newPsw2, SimpleCallBack<Object> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put("oldpassword", oldPsw);
            jsonObject.put("newpassword", newPsw);
            jsonObject.put("confirmpassword", newPsw2);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1005, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 修改手机号
     *
     * @param observer observer
     */
    public void changeMobile(String mobile, String code, SimpleCallBack<Object> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put("newmobile", mobile);
            jsonObject.put("captcha", code);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1009, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 修改头像
     *
     * @param imageUrl 头像地址
     * @param observer observer
     */
    public void changeHeadImage(String imageUrl, SimpleCallBack<Object> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put("avatar_url", imageUrl);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1008, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 智库分类
     *
     * @param observer observer
     */
    public void thinkTankCat(SimpleCallBack<ThinkTankEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1029, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 智库列表
     *
     * @param observer observer
     */
    public void thinkTankList(String category_id, int page, int pageSize, SimpleCallBack<ThinkTankItemEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST.PAGE, page);
            jsonObject.put("category_id", category_id);
            jsonObject.put(HttpCST.PAGE_SIZE, pageSize);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1016, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }


    /**
     * 上传头像
     *
     * @param file     图片
     * @param observer observer
     */
    public void uploadImage(File file, SimpleCallBack<UploadHeadBean> observer) {
        EasyHttp.getInstance().setEncrypt(false);
        EasyHttp.post(HttpCST.UPLOAD_URL)
                .params(HttpCST.ACCESS_TOKEN, App.model.getAccess_token())
                .params("image", file, file.getName(), null).execute(observer);
    }

    /**
     * 退出登录
     *
     * @param observer observer
     */
    public void loginOut(SimpleCallBack<Object> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1034, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }


    /**
     * 提交建议
     */
    public void feedback(String content, SimpleCallBack<Object> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put("content", content);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1013, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 联盟商
     *
     * @param observer observer
     */
    public void allianceBusiness(SimpleCallBack<AllianceBusinessBean> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_UNION, HttpCST.INTFC_1006, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 提现列表
     *
     * @param observer observer
     */
    public void recordList(int page, int pageSize, SimpleCallBack<RecordListBean> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST.PAGE, page);
            jsonObject.put(HttpCST.PAGE_SIZE, pageSize);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_WITHDRAW, HttpCST.INTFC_1002, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 申请提现
     *
     * @param observer observer
     */
    public void putForward(String type, String account, String name, String money, SimpleCallBack<Object> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put("type", type);
            jsonObject.put("account", account);
            jsonObject.put("name", name);
            jsonObject.put("money", money);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_WITHDRAW, HttpCST.INTFC_1001, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 编辑银行卡
     *
     * @param observer observer
     */
    public void editBankCard(String _id, String user_name, String bank_name, String bank_num, String is_def, SimpleCallBack<Object> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST._id, _id);
            jsonObject.put("user_name", user_name);
            jsonObject.put("bank_name", bank_name);
            jsonObject.put("bank_num", bank_num);
            jsonObject.put("is_def", is_def);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1024, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 修改设置
     * 修改的type（1：姓名，2：邮箱，3：简介,4:服务项目）
     */
    public void changeSetting(int type, String content, SimpleCallBack<Object> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST.TYPE, type);
            jsonObject.put("content", content);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1007, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 卡券列表
     *
     * @param observer observer
     */
    public void cardList(int page, int pageSize, SimpleCallBack<CardCenterEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST.PAGE, page);
            jsonObject.put(HttpCST.PAGE_SIZE, pageSize);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_UNION, HttpCST.INTFC_1003, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }


    /**
     * 卡券使用说明
     */
    public void cardInfo(SimpleCallBack<CardInfoEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_UNION, HttpCST.INTFC_1004, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 关于我们
     */
    public void aboutUs(SimpleCallBack<AboutUsEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_UTILS, HttpCST.INTFC_1002, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 服务协议
     */
    public void service(SimpleCallBack<ServiceEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_UTILS, HttpCST.INTFC_1004, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 使用说明
     */
    public void instructions(SimpleCallBack<InstructionEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_UTILS, HttpCST.INTFC_1006, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 积分说明
     */
    public void jifensm(SimpleCallBack<JiFenEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_CREDIT, HttpCST.INTFC_1003, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 注册协议
     */
    public void getRegisterAgreement(SimpleCallBack<JiFenEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            //jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_UTILS, HttpCST.INTFC_1008, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 我的活动
     *
     * @param observer observer
     */
    public void myActivities(String type, int page, int pageSize, SimpleCallBack<MyActivitiesEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST.PAGE, page);
            jsonObject.put(HttpCST.TYPE, type);
            jsonObject.put(HttpCST.PAGE_SIZE, pageSize);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_ACTIVE, HttpCST.INTFC_1005, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 积分首页
     *
     * @param observer observer
     */
    public void jifen(SimpleCallBack<GrowthIntegralEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_CREDIT, HttpCST.INTFC_1001, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 积分明细
     *
     * @param observer observer
     */
    public void jifenmx(int page, int pageSize, SimpleCallBack<IntegralDetailListEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST.PAGE, page);
            jsonObject.put(HttpCST.PAGE_SIZE, pageSize);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_CREDIT, HttpCST.INTFC_1002, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 积分排行榜
     *
     * @param observer observer
     */
    public void rankingList(int page, int pageSize, SimpleCallBack<IntegralRankingListEntity> observer) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_CREDIT, HttpCST.INTFC_1004, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(observer);
    }

    /**
     * 修改钱包密码
     *
     * @param captcha         验证码
     * @param newpassword     新密码
     * @param confirmpassword 确认密码
     * @param pCallBack       回调
     */
    public void walletModifyPass(String captcha, String newpassword, String confirmpassword, SimpleCallBack<Object> pCallBack) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put("captcha", captcha);
            jsonObject.put("newpassword", newpassword);
            jsonObject.put("confirmpassword", confirmpassword);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_MEMBER, HttpCST.INTFC_1028, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(pCallBack);
    }
}
